#include "front.h"#include "manager.h"
front::front(){}
front::~front(){	close();}
void front::setFd(int fd){
	tcp.__setFdForPrivate(fd);	tcp.setNonBlock();}
void front::setManager(manager *m ){
	boss = m;}

void front::input(){	int len = tcp.recv(buffer, 10240);	buffer[len] = 0;
	if( len == 0 ){		if( tcp.getRevStatus() < 0 ){			boss->closeConn();				dll.handle_close(1);		}
		return ;
	}else if (len < 0){		boss->closeConn();
		return ;
	}else if( len > 0 ){		r_buf.pushData(buffer, len);		int mod = 0;//0 to handle , 1:tran		int read_len = r_buf.size();		int cnt = 0;		while( read_len > 0 ){			cnt ++;			int packetLen = dll.handle_input(r_buf.getBufferPtr(), r_buf.size(), boss);			//int packetLen = dll.handle_input(r_buf.getBufferPtr(), r_buf.size(), mod, boss);			if( packetLen == 0 ){				break;			}else if( packetLen < 0 ){				boss->closeConn();				return ;			}			int ret = dll.handle_process(r_buf.getBufferPtr(), packetLen, boss);			if( ret < 0 ){				boss->closeConn();				return ;			}			r_buf.skipData(packetLen);			/*			if ( ret > 0 ){				if( mod == 1 ){					boss->sendToBack(r_buf.getBufferPtr(), ret);				}else{				}				r_buf.skipData(ret);			}*/			read_len = r_buf.size();		}	}}
void front::output(){	if (send_only_buffer() < 0){		boss->closeConn();		return ;	}}int front::send_only_buffer(){	if( tcp.getFd() < 0 ){		return -1;	}	if( w_buf.size() <= 0 ){// TODO if < 0 the buffer is error now		return 0;	}	int ret = tcp.send(w_buf.getBufferPtr(), w_buf.size());	if( ret < 0 ){		return ret;	}else if( ret == 0 ){		return 0;	}	if( ret > w_buf.size() ){		return -1;	}	w_buf.skipData(ret);	if( w_buf.size() <= 0 ){		_closeWrite();	}	return 0;}int front::send(const char* msg, int msg_len){	int ret = 0;	if( w_buf.size() > 0){		w_buf.pushData(msg, msg_len);		ret = tcp.send(w_buf.getBufferPtr(), w_buf.size());		if( ret <= 0 ){			return ret;		}else{			if( ret <= w_buf.size() ){				w_buf.skipData(ret);			}		}	} else{		ret = tcp.send(msg, msg_len);		if( ret < 0 ){			return ret;		}else if( ret < msg_len ){			w_buf.pushData(msg+ret, msg_len-ret);		}	}		if( w_buf.size() > 0 ){		_openWrite();	}		return ret;	}
void front::close(){	tcp.close();}void front::_openWrite(){	event_del(&netEvent); 	event_set(&netEvent, tcp.getFd(), EV_READ | EV_WRITE | EV_PERSIST , dataInput, (void *)this);  	event_add(&netEvent, NULL); }void front::_closeWrite(){	event_del(&netEvent); 	event_set(&netEvent, tcp.getFd(), EV_READ | EV_PERSIST , dataInput, (void *)this);  	event_add(&netEvent, NULL); }
